package api

import (
	"fmt"
	"jgt-oper/src/bll"
	"jgt-oper/src/context"
	"jgt-oper/src/schema"
	"jgt-oper/src/util"
)

// AuthCode 认证码管理
type AuthCode struct {
	AuthCodeBll   *bll.AuthCode   `inject:""`
	EnterpriseBll *bll.Enterprise `inject:""`
	UserBll       *bll.User       `inject:""`
}

// Check 检查认证码
func (a *AuthCode) Check(ctx *context.Context) error {
	code := ctx.Query("code")
	if code == "" {
		return ctx.ResErrBadRequest(fmt.Errorf("无效的请求参数"))
	}

	item, err := a.AuthCodeBll.Check(code)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	eitem, err := a.EnterpriseBll.Get(item.EnterpriseID)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	} else if eitem == nil {
		return ctx.ResErrInternalServer(fmt.Errorf("无效的企业"))
	}

	codeType := "企业管理员"
	if v := item.CodeType; v == "30" {
		codeType = "企业员工"
	}
	data := map[string]interface{}{
		"record_id":       item.RecordID,
		"enterprise_name": eitem.Name,
		"code_type":       codeType,
	}
	return ctx.ResSuccess(data)
}

// QueryPage 查询分页数据
func (a *AuthCode) QueryPage(ctx *context.Context) error {
	user, err := a.UserBll.Get(ctx.GetUserID())
	if err != nil {
		return ctx.ResErrBadRequest(err)
	} else if user == nil {
		return ctx.ResErrBadRequest(fmt.Errorf("无效的用户"))
	}

	params := schema.AuthCodeQueryParam{
		EnterpriseID: user.EnterpriseID,
		LastID:       util.S(ctx.Query("lastID")).Int64(),
	}

	_, items, err := a.AuthCodeBll.QueryPage(params, 0, ctx.GetPageSize())
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	for i := 0; i < len(items); i++ {
		eitem, _ := a.EnterpriseBll.Get(items[i].EnterpriseID)
		if eitem != nil {
			items[i].EnterpriseName = eitem.Name
		}
	}

	return ctx.ResSuccess(items)
}

// QueryHistoryPage 查询历史分页数据
func (a *AuthCode) QueryHistoryPage(ctx *context.Context) error {
	params := schema.AuthHistoryQueryParam{
		CodeID:     ctx.Param("id"),
		LastID:     util.S(ctx.Query("lastID")).Int64(),
		RealName:   ctx.Query("real_name"),
		Tel:        ctx.Query("tel"),
		QueryValue: ctx.Query("query_value"),
	}

	_, items, err := a.AuthCodeBll.QueryHistoryPage(params, 0, ctx.GetPageSize())
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	return ctx.ResSuccess(items)
}

// Get 精确查询数据
func (a *AuthCode) Get(ctx *context.Context) error {
	item, err := a.AuthCodeBll.Get(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	} else if item != nil {
		eitem, _ := a.EnterpriseBll.Get(item.EnterpriseID)
		if eitem != nil {
			item.EnterpriseName = eitem.Name
		}
	}
	return ctx.ResSuccess(item)
}

// Create 创建数据
func (a *AuthCode) Create(ctx *context.Context) error {
	user, err := a.UserBll.Get(ctx.GetUserID())
	if err != nil {
		return ctx.ResErrBadRequest(err)
	} else if user == nil {
		return ctx.ResErrBadRequest(fmt.Errorf("无效的用户"))
	}

	var item schema.AuthCode
	if err := ctx.ParseJSON(&item); err != nil {
		return ctx.ResErrBadRequest(err)
	}

	if t := util.GetUserTypeFlag(user.UserType); t != "40" {
		return ctx.ResErrBadRequest(fmt.Errorf("没有创建认证码的权限"))
	}

	item.EnterpriseID = user.EnterpriseID
	if item.EnterpriseID == "" {
		return ctx.ResErrBadRequest(fmt.Errorf("请先选择企业"))
	}
	item.Creator = ctx.GetUserID()

	err = a.AuthCodeBll.Create(&item)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	dataItem, err := a.AuthCodeBll.Get(item.RecordID)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	return ctx.ResSuccess(dataItem)
}

// Delete 删除数据
func (a *AuthCode) Delete(ctx *context.Context) error {
	err := a.AuthCodeBll.Delete(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// Disable 作废数据
func (a *AuthCode) Disable(ctx *context.Context) error {
	err := a.AuthCodeBll.UpdateStatus(ctx.Param("id"), 2)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}
